package com.dl7.mvp.module.video.main;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.dl7.mvp.R;
import com.dl7.mvp.adapter.MyFragmentPagerAdapter;
import com.dl7.mvp.injector.components.DaggerVideoMainComponent;
import com.dl7.mvp.injector.modules.VideoMainModule;
import com.dl7.mvp.local.table.AlbumsListBean;
import com.dl7.mvp.module.base.BaseFragment;
import com.dl7.mvp.module.base.ILoadDataView;
import com.dl7.mvp.module.base.IRxBusPresenter;
import com.dl7.mvp.module.video.list.AlbumsListFragment;
import com.dl7.mvp.module.video.list.RecommendFragment;
import com.dl7.mvp.rxbus.event.VideoEvent;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import rx.functions.Action1;

/**
 * video主界面
 */
public class VideoMainFragment extends BaseFragment<IRxBusPresenter> implements ILoadDataView<List<AlbumsListBean.DataBean>> {
    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    @Inject
    MyFragmentPagerAdapter mPagerAdapter;

    @BindView(R.id.table_layout)
    TabLayout mTableLayout;

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_video_main;
    }

    @Override
    protected void initInjector() {
        DaggerVideoMainComponent.builder()
                .applicationComponent(getAppComponent())
                .videoMainModule(new VideoMainModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initViews() {
        mPresenter.registerRxBus(VideoEvent.class, new Action1<VideoEvent>() {
            @Override
            public void call(VideoEvent videoEvent) {
                if (videoEvent.checkStatus == VideoEvent.CHECK_INVALID) {
                    mPresenter.getData();
                }
            }
        });
        mTableLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTableLayout.setupWithViewPager(mViewPager);
    }

    @Override
    protected void updateViews() {
        mPresenter.getData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.unregisterRxBus();
    }

    @Override
    public void loadData(List<AlbumsListBean.DataBean> categoryList) {
        if (categoryList == null) {
            return;
        }
        ArrayList<Fragment> fragments = new ArrayList<>();
        List<String> titles = new ArrayList<>();
        fragments.add(RecommendFragment.newInstance());
        titles.add("推荐");
        for (int i = 0; i < categoryList.size(); i++) {
            AlbumsListBean.DataBean categoryBean = categoryList.get(i);
            titles.add(categoryBean.getName());
            fragments.add(AlbumsListFragment.newInstance(categoryBean.getId()));
        }
        mPagerAdapter.setItems(fragments, titles);
        mViewPager.setAdapter(mPagerAdapter);
    }

    @Override
    public void loadMoreData(List<AlbumsListBean.DataBean> data) {

    }

    @Override
    public void loadNoData() {

    }
}
